<?php
/*
 * Con_daily 日报
 * 2012-05-09
 * abans@qq.com
 */
class Con_work extends Acan{
	public $defaultAct='daily';
	public $Conread = '办公';
	public $Actread = array('daily'=>'日报','daily_recvlist'=>'接受日报列表','daily_add'=>'新建日报','daily_view'=>'已发日报显示','daily_new'=>'新日报检查');
	function __construct($ainfo=null){
		parent::__construct($ainfo=null);
		$this->atitle = $this->Actread[$this->Aact];
		$this->uid = Afun_uid();
		$this->session = Afun_session();
		$this->today = date("Y-m-d");
	}
	//日报
	function Act_daily(){
		$this->model = Afun_db()->model(Acan_table_oa_daily);
		$obj = Afun_acan_new('list');
		$column_json = $obj->form_json($this->model);
		
		$column = array("星期日","星期一","星期二","星期三","星期四","星期五","星期六");
		$month = date("Y-m");
		if(empty($_GET['month'])){
			$_GET['month'] = $month;
		}else{
			$month = $_GET['month'];
		}
		$fristday = $month.'-1';
		$lastday = $month.date("-t",strtotime($month));
		$daynum = date("t",strtotime($month));//这个月的天数
		//--查询发送日报
		$con->select = "a.*,COUNT(a.id) as sendnum";
		$con->where = "a.uid=".$this->uid." and a.date LIKE '$month%'";
		$con->group = "a.date";
		$model = $this->model->findAll($con);
		foreach($model as $k=>$v){
			$mindex[$v['date']] = $v;
		}
		unset($con);
		//--查询接受日报
		$con->select = "a.date,COUNT(a.id) as recvnum";
		$con->where = "a.date LIKE '$month%' and a.recv LIKE '%\"".$this->uid."\"%'";
		$con->group = "a.date";
		$model = $this->model->findAll($con);
		foreach($model as $k=>$v){
			$mindex[$v['date']]['recvnum'] = $v['recvnum'];
		}
		for($i=1;$i<=$daynum;$i++){
			$data['day'] = $i;
			if($data['day']<10){
				$data['day'] = '0'.$data['day'];
			}
			$day = $month.'-'.$data['day'];
			$week = date('W',strtotime($day)+86400);//一年中的第几周
			$weekday = date('w',strtotime($day));//一周中的第几天
			$data['date'] = $day;
			$data['sendnum'] = 0;
			$data['recvnum'] = 0;
			$data['bgcolor'] = "#BBD2F8";
			$data['daycolor'] = "#666";
			//--如果这天有发送日报
			if(isset($mindex[$day]['id'])){
				$data['id'] = $mindex[$day]['id'];
				$data['sendnum'] = $mindex[$day]['sendnum'];
			}
			//--如果这天有接受日报
			if(isset($mindex[$day]['recvnum'])){
				$data['recvnum'] = $mindex[$day]['recvnum'];
			}
			//--工作日没写日报的日子
			if($data['sendnum']==0 && (0<$weekday && $weekday<6) && strtotime($day)<=strtotime($this->today)){
				$data['bgcolor'] = "#FC3";
				$data['daycolor'] = "red";
			}elseif($data['sendnum']>0){//写日报的日子
				$data['bgcolor'] = "#EFEFEF";
				$data['daycolor'] = "#690";
			}
			$datas[$week][$weekday] = $data;
		}
		$weekstart = date('W',strtotime($fristday)+86400);//一个月第一周
		$weekend = date('W',strtotime($lastday)+86400);//一个月最后一周
		for($i=$weekstart;$i<=$weekend;$i++){
			if(is_int($i) && $i<10){
				$i = '0'.$i;
			}
			for($d=0;$d<7;$d++){
				$list[$i][$d] = $datas[$i][$d];
			}
		}
		include $this->Atpl();
	}
	function Act_daily_recvlist(){
		$this->model = Afun_db()->model(Acan_table_oa_daily);
		$obj = Afun_acan_new('list');
		$columnrr = array('title'=>'标题','date'=>'日期','group'=>'部门');
		$column = $obj->column($this->model,$columnrr,$_POST['acolumn']);
		//--查询接受日报
		if(!empty($_GET['date'])){
			$con->where = "a.date ='".$_GET['date']."'";
		}else{
			$con->where = "1";
		}
		$con->select = "a.id,a.date,a.read,a.title,b.group";
		if($this->uid>1){
			//--只能查看发给自己的日报
			$con->where .= " and a.recv LIKE '%\"".$this->uid."\"%'";
		}
		$con->join = "LEFT JOIN `".Acan_table_user_group."` b ON a.uid=b.uid and b.appid=".Acan_app_id;
		$page->theurl = $this->Aactr.'-'.$this->id;
		$page->by = $this->model->key_name;
		$page->page = '20';
		$page->counts = $this->model->count($con);	//统计总数目
		$pages = Afun_apage($page);
		if($pages->by=='group'){
			$con->order = "b.$pages->orderby";
		}else{
			$con->order = "a.$pages->orderby";
		}
		$con->limit = $pages->limit;
		$list = $this->model->findAll($con);
		$this->model->enum['group'] = Afunc_group()->enum();
		include $this->Atpl();
	}
	//新建日报
	function Act_daily_add(){
		//$con->join = "left join `".Acan_table_oa_daily_box."` b ON a.id=b.daily_id";
		$model = Afun_db()->model(Acan_table_oa_daily);
		$con->where = "uid=".$this->uid." and date='".$this->today."'";
		$data = $model->find($con);
		if(!$data){
			$data['title'] = "日报-".$this->session['user'].date("-Ymd");
		}
		if(empty($data['note'])){
			$data['note'] = file_get_contents(A_ROOT.'/atpl/'.Acan_app_name.'/'.$this->Amod.'/daily_tpl.htm');
		}
		if($_POST){
			$save = $_POST['save'];
			$recvs = json_decode($save['recv']);
			if(is_array($recvs) || is_object($recvs)){
				foreach ($recvs as $k => $v) {
					$recv[] = $v;
				}
			}
			$save['recv'] = Ajson_encode($recv);
			$save['date'] = $this->today;
			$save['uid'] = $this->uid;
			$model->save($save);
			//$send = $this->send_mail($recv,$save);
			//$rs->msg = "保存成功,".$send->msg;
			$rs->msg = "保存成功";
			//$rs->status = $send->status;
			$rs->status = 1;
			Afun_json($rs);
		}
		$userdata = Afunc_group()->userlist();
		$recvs = json_decode($data['recv']);
		if(is_array($recvs) || is_object($recvs)){
			foreach ($recvs as $k => $v) {
				if(empty($recvdata)){
					$recvdata = $userdata[$v];
				}else{
					$recvdata .= ','.$userdata[$v];
				}
			}
		}
		$json->user = $userdata;
		$json->data = Afunc_group()->usergrouplist();
		unset($json->data[99]);
		$json->enum = Afunc_group()->enum();
		$userjson = "<script>var userdata = ".Ajson_encode($json)."; </script>";
		
		include $this->Atpl();
	}
	//发送邮件
	function send_mail($recv,$save){
		$sqlin = Afun_arr_unisql($recv,'int');
		$con->select = "a.mail,a.username";
		$con->where = "a.uid in ($sqlin)";
		$model = Afun_db()->model(Acan_table_user)->findAll($con);
		foreach($model as $k=>$v){
			$tomail[$v['mail']] = $v['username'];
		}
		$toname = '';
		preg_match_all('/<a ([\w\s\d\/\%\.\?\+ "=:_-]+?)>([\x{4e00}-\x{9fa5}\w\s\d\/\.\%\?\+ "=:_-]*?)<\/a>/u',$save['note'],$arr);
		//--附件发送
		/*foreach($arr[0] as $k=>$v){
			$attach[$k]['name'] = $arr[2][$k];
			preg_match_all('/href="([\w\d\s\/\.]+?)"/',$arr[1][$k],$arr2);
			$attach[$k]['path'] = realpath(A_ROOT.$arr2[1][0]);
		}*/
		$body = str_replace('href="/upfile','href="http://'.$_SERVER["SERVER_ADDR"].'/upfile',$save['note']);
		$status = Afun_mail($tomail,$toname,$save['title'],$body);
		if($status==true){
			$rs->msg = '邮件发送成功！';
			$rs->status = 1;
		}else{
			$rs->msg = '邮件发送失败！';
			$rs->status = 2;
		}
		return $rs;
	}
	//--获取日报内容
	function Act_daily_view(){
		$this->model = Afun_db()->model(Acan_table_oa_daily);
		$data = $this->model->ofindPk($this->id);
		//--阅读标记
		if($this->uid>1 && !preg_match("/\"".$this->uid."\"/i",$data->read)){
			$read = json_decode($data->read);
			if(is_array($read) || is_object($read)){
				foreach($read as $k=>$v){
					$arr[] = $v;
				}
			}
			$arr[] = $this->uid;
			$data->read = Ajson_encode($arr);
			$this->model->updatePk(array('read'=>$data->read),$this->id);
		}
		Afun_json($data);
	}
	//--获取新日报
	function Act_daily_new(){
		$this->model = Afun_db()->model(Acan_table_oa_daily);
		$con->select = "count(*) as num";
		$con->where = "a.recv LIKE '%\"".$this->uid."\"%' and a.read NOT LIKE '%\"".$this->uid."\"%'";
		$data = $this->model->ofind($con);
		Afun_json($data);
	}
}

?>